import asunit.framework.TestCase;

import mockAS2.Sandbox;
import mockAS2.SpyMethod;
import mockAS2.SpyObject;
import mockAS2.mocks.ClassToBeMocked;

/**
 * Copyright (c) 2012 Gary Higham
 * Released under the MIT license
 * https://code.google.com/p/mock-as2/wiki/License
 *
 * Version: v0.1
 */
class mockAS2.SandboxTest extends TestCase {
	private var className : String = "mockAS2.SandboxTest"; 
	private var sandbox : Sandbox;
	private var dummy : ClassToBeMocked;
	
	public function SandboxTest(testMethod : String) {
		super(testMethod);
	}
	
	public function setUp() : Void {
		sandbox = new Sandbox();
		dummy = new ClassToBeMocked();	
	}
	
	public function testSandboxCanRestoreSingleItemAdded() : Void {
		var spyHasSpyBeenRestored : SpyMethod = new SpyMethod(createSpyOnObject(), "restore");
		sandbox.restore();
		assertTrue("Spy on Object has been restored", spyHasSpyBeenRestored.calledOnce());
	}
	
	public function testSandboxCanRestoreMultipleItemsAdded() : Void {
		var spyHasSpyBeenRestored : SpyMethod = new SpyMethod(createSpyOnObject(), "restore");
		var spyOnMethodRestored : SpyMethod = new SpyMethod(createSpyOnMethod(), "restore");
		sandbox.restore();
		assertTrue("Spy on Object has been restored", spyHasSpyBeenRestored.calledOnce());
		assertTrue("Spy on Method has been restored", spyOnMethodRestored.calledOnce());
	}
	
	private function createSpyOnObject() : SpyObject {
		var spyOnObject : SpyObject = new SpyObject(this, "dummy");
		sandbox.addItem(spyOnObject);
		return spyOnObject;
	}
	
	private function createSpyOnMethod() : SpyMethod {
		var spyOnMethod : SpyMethod = new SpyMethod(dummy, "return1");
		sandbox.addItem(spyOnMethod);
		return spyOnMethod;
	}
}
